package com.yiren.dbaa.module.practice_set.site.presenter;

import android.app.Activity;

import com.yiren.backstage.currency.entity.SiteEntity;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.module.practice_set.site.bean.CityBean;
import com.yiren.dbaa.module.practice_set.site.bean.ProvinceBean;
import com.yiren.dbaa.module.practice_set.site.contract.AddSiteRuleContract;
import com.yiren.dbaa.module.practice_set.site.model.AddSiteRuleModel;
import com.yiren.dbaa.view.dialog.LoadDialog;
import com.yiren.dbaa.view.toast.MessageBox;

import java.util.List;

/**
 * @author lwb
 * @date : 2022/9/23 10:00
 * @description: 新建规则
 */
public class AddSiteRulePresenter implements AddSiteRuleContract.Presenter {

    private Activity activity;
    private AddSiteRuleContract.Model addSiteRuleModel;

    public AddSiteRulePresenter(Activity activity) {
        this.activity = activity;
        addSiteRuleModel = new AddSiteRuleModel(activity);
    }

    @Override
    public void loadShareSiteRule(AddSiteRuleContract.AddSiteRuleView addSiteRuleView, String city, String user) {
        LoadDialog.show(activity,"正在查询中...");
        addSiteRuleModel.getShareSiteRule(city, user, new CommonCallBack() {
            @Override
            public void success(Object result) {
                LoadDialog.dismiss(activity);
                List<SiteEntity> shareSites = (List<SiteEntity>) result;
                addSiteRuleView.showShareSiteRuleList(shareSites);
                super.success(result);
            }

            @Override
            public void fail(String error) {
                LoadDialog.dismiss(activity);
                MessageBox.show(activity, error);
            }
        });

    }
    @Override
    public void loadProvinceCity(AddSiteRuleContract.AddSiteRuleView addSiteRuleView) {
        activity.runOnUiThread(()-> LoadDialog.show(activity,"数据正在加载中..."));
        addSiteRuleModel.getProvince(new CommonCallBack() {
            @Override
            public void success(Object result) {
               List<ProvinceBean>   provinceBeanList = (List<ProvinceBean>) result;
               for (int i = 0; i<provinceBeanList.size(); i++){
                   final int k = i;
                   ProvinceBean province = provinceBeanList.get(i);
                   addSiteRuleModel.getCity(province.getId(), new CommonCallBack() {
                       @Override
                       public void success(Object result) {
                           province.setCityBeanList((List<CityBean>) result);
                           provinceBeanList.set(k,province);
                           if (k==33){
                               addSiteRuleView.showProvinceCity(provinceBeanList);
                           }
                       }
                   });
               }
            }

            @Override
            public void fail(String error) {
              MessageBox.show(activity,error);
            }
        });


    }


}
